package com.emuporium.gymrat.server.log;

import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.emuporium.gymrat.server.data.Entry;
import com.emuporium.gymrat.server.data.Equipment;
import com.emuporium.gymrat.server.data.ExerciseDB;
import com.emuporium.gymrat.server.data.Routine;
import com.emuporium.gymrat.server.data.RoutineDB;
import com.emuporium.gymrat.server.data.User;
import com.emuporium.gymrat.server.data.UserDB;

@SuppressWarnings("serial")
public class LogAddEntryServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		int routineId = new Integer(request.getParameter("routineid"));
		User user = UserDB.selectUser(request.getRemoteUser());
		Routine routine = RoutineDB.selectRoutine(routineId);
		request.setAttribute("routine", routine);
		request.setAttribute("equipment", ExerciseDB.selectAllEquipment(0));
		LinkedHashMap<Integer, ArrayList<Equipment>> entryEquip = new LinkedHashMap<Integer, ArrayList<Equipment>>();

		for (Entry e : routine.getEntries()) {
			ArrayList<Equipment> equipment = ExerciseDB.selectEquipment(e
					.getExerciseId(), user.getId());
			entryEquip.put(e.getId(), equipment);
		}

		request.setAttribute("entryEquip", entryEquip);
		String url = "/log/addLogEntry.jsp";
		RequestDispatcher dispatcher = getServletContext()
				.getRequestDispatcher(url);
		dispatcher.forward(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}
}
